<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Video extends MY_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();
		
		//Set Target Asset Url
		$this->config->set_item('base_asset_url', $this->config->item('base_video_asset_url'));
				
		//Google Analytics ID
		$this->data['ga_id'] = 13;		
		
		//Load model
		$this->load->model('video_model');
				
		//Load Video Script
		$this->data['js'][] = "videoScript.js";
		$this->data['css'][] = "videoStyle.css";
		
		 //Load Library
  		$this->load->library('pagination');
		
		//cookie
		$this->data['mode'] = get_cookie('ch_mn');
		if(empty($this->data['mode'])) $this->data['mode'] = 3;
		
		//Set ID 			
		$this->data['group'] = array(
			'mv' => 1,
			'concert' => 4,
			'interview' => 6,
			'behind_scence' => 3,
			'you2playtv' => array(
				'is_tv_show' => '1'
			)		
		);
		$this->data['filter'] = array(
			'new_released' => 'วีดีโอใหม่ล่าสุด',
			'recommend' => 'วีดีโอแนะนำ',
			'hit' => 'วีดีโอคนดูมากที่สุด'
		);	
		$this->data['media_type'] = array(
			'thai' => 1,
			'inter' => 2,
			'asian' => 3			
		);
		
		//Set Category Hawkeye
		$this->data['category_hawkeye'] = array(
			'1' => array('Music Video'),
			'3' => array('Behind the scene'),
			'4' => array('Concert'),
			'6' => array('Interview'),
			'10' => array('Entertainment'), //69
			'26' => array('Movie'), //At The Movie
			'24' => array('Entertainment'), //Brand New
			'30' => array('Entertainment'), //Breezer The Game
			'18' => array('Entertainment'), //Buyz plus the hoods
			'22' => array('Reality Show'), //Chang Music Contest
			'25' => array('Travel'), //Singto resort
			'23' => array('Fashion','Entertainment'), //Spy inspiring
			'17' => array('Reality Show'), //Thailand Most Famous
			'16' => array('Eat &amp; Drink'), //The Bling Kitchen
			'21' => array('News','Entertainment'), //What
			'29' => array('News','Entertainment'), //Whats happening
			'13' => array('Entertainment'), //You2Play TV
			'15' => array('Entertainment'), //ของมัน Chill
			'20' => array('Music','Fashion'), //ครัวซอง
			'27' => array('Travel'), //ฟาวโลด
			'12' => array('Gossip'), //ยุ่งเรื่องชาวบ้าน
			'19' => array('Music','Entertainment'), //สยามพาเหรด
			'9' => array('Entertainment'), //อยากเล่นเป็น VJ
			'28' => array('Travel','Entertainment') //เด็กมันแรงส์
		);
		
		//Set SEO Tag
		$this->data['seo_title'] = 'วีดีโอ คลิปวีดีโอ MV สัมภาษณ์ เบื้องหลัง คอนเสิร์ต ศิลปิน ดารา นักร้อง | You2Play.com';
		$this->data['seo_description'] = 'วีดีโอ คลิปวีดีโอ MV สัมภาษณ์ เบื้องหลัง คอนเสิร์ต เกี่ยวกับ ศิลปิน ดารา นักร้อง';
		$this->data['seo_keyword'] = 'วีดีโอ, คลิปวีดีโอ, MV, คอนเสิร์ต, สัมภาษณ์';					
    }
	
	/*Home*/	 
	public function index()
	{	
		//Load Module
		$this->data['module'] = $this->module('video');
		$this->data['cat_header'] = $this->video_cat_header();
		if ( !$this->data['videomv'] = $this->memcache->get('dataVdoMv')):
			$this->data['videomv'] = $this->db->select('t.video_id,t.is_relate_song,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('t.video_id','DESC')->
			get_where('video as t',"t.video_type_id = 1 AND t.is_publish = '1' AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW())",12)->
			result();
			foreach($this->data['videomv'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id,'t.is_default' => '1'))
					->result();				
				endif;					
			endforeach;
			$this->memcache->set('dataVdoMv', $this->data['videomv'], $this->memcache_time);
		endif;
		
		if ( !$this->data['videoConcert'] = $this->memcache->get('dataVdoConcert')):
			$this->data['videoConcert'] = $this->db->select('t.video_id,t.is_relate_song,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('t.video_id','DESC')->
			get_where('video as t',"t.video_type_id = 4 AND t.is_publish = '1' AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW())",8)->
			result();
			foreach($this->data['videoConcert'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id,'t.is_default' => '1'))
					->result();				
				endif;
			endforeach;
			$this->memcache->set('dataVdoConcert', $this->data['videoConcert'], $this->memcache_time);
		endif;

		if ( !$this->data['videoView'] = $this->memcache->get('dataVdoView')):
			$this->data['videoView'] = $this->db->select('t.video_id,t.is_relate_song,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('t.video_id','DESC')->
			get_where('video as t',"t.video_type_id = 6 AND t.is_publish = '1' AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW())",8)->
			result();
			foreach($this->data['videoView'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id,'t.is_default' => '1'))
					->result();				
				endif;
			endforeach;
			$this->memcache->set('dataVdoView', $this->data['videoView'], $this->memcache_time);
		endif;
		
		if ( !$this->data['videoBehind'] = $this->memcache->get('dataVdoBehind')):
			$this->data['videoBehind'] = $this->db->select('t.video_id,t.is_relate_song,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('t.video_id','DESC')->
			get_where('video as t',"t.video_type_id = 3 AND t.is_publish = '1' AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW())",8)->
			result();
			foreach($this->data['videoBehind'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id,'t.is_default' => '1'))
					->result();				
				endif;
			endforeach;
			$this->memcache->set('dataVdoBehind', $this->data['videoBehind'], $this->memcache_time);
		endif;

		if ( !$this->data['videotv'] = $this->memcache->get('dataVdoTv')):
			$this->data['videotv'] = $this->db->select('t.video_id,t.is_relate_song,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			join('video_type as k',"k.video_type_id=t.video_type_id",'INNER')->
			order_by('t.video_id','DESC')->
			get_where('video as t',"k.is_tv_show = '1' AND t.is_publish = '1' AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW())",8)->
			result();
			foreach($this->data['videotv'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id,'t.is_default' => '1'))
					->result();				
				endif;
			endforeach;
			$this->memcache->set('dataVdoTv', $this->data['videotv'], $this->memcache_time);
		endif;		
		
		$this->data['content'] = $this->router->class."/index";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		
	}
	
	public function ajax_set_cookies(){
		$value = $this->input->post('ch_mn');
		if(empty($value)) $this->page_missing();
		$mycookie = array(
		   'name'  => 'ch_mn',
		   'value' => $value,
		   'expire'=> '86500'
  		);
		set_cookie($mycookie);
	}
	
	public function ajax_search()
	{
		$txt = $this->input->post('txt');
		$this->data['searchAuto'] = $this->db->select('t.video_name,t.video_id')->
		join('video as j',"j.video_id=t.video_id",'INNER')->
		where("t.video_name LIKE '".$txt."%'")->
		get('video_info as t',10)->
		result();
		
		$this->load->view("video/ajax_search", $this->data);
	}
	
	public function ajax_search_result()
	{
		$this->load->library('solr');
		$txt = $this->solr->escapeSolrValue($this->input->get('txt'));
		$SQL = 'video_name_s:'.$txt.'* OR video_name_s:'.$txt;

		$config['base_url'] = base_url().'ajax_search_result?txt='.$txt.query_string(array('per_page','txt'));
		$config['total_rows'] = $this->solr->query($SQL)->count_all_results();
		$config['per_page'] = 20;
		$config['num_links'] = 5;
		$config['first_link'] = '<span class="arrows first">&laquo;</span>';
  		$config['next_link'] = '<span class="arrows next">&gt;</span>';
  		$config['prev_link'] = '<span class="arrows prev">&lt;</span>';
  		$config['last_link'] = '<span class="arrows last">&raquo;</span>';
		$config['page_query_string'] = TRUE;
		$config['query_string_segment'] = 'p';
		$this->pagination->initialize($config);
		$this->data['pager'] = $this->pagination->create_links();

		$this->data['dataResult'] = $this->solr->query($SQL)->limit(intval($this->input->get('p')),$config['per_page'])->
		order_by(array('last_modified' => 'DESC'))->result();
		foreach($this->data['dataResult'] as $value):
			$value->video_id = substr($value->id,6);
			/*Get Artist Info*/
			if(!empty($value->artist_id_i_arr)):
				$artistQuery = '';
				if(is_array($value->artist_id_i_arr)):
					foreach($value->artist_id_i_arr as $artist_id):
						$artistQuery .= 'id:artist_'.$artist_id.' OR ';
					endforeach;
					$artistQuery = rtrim($artistQuery,' OR ');
				else:
					$artistQuery = 'id:artist_'.$value->artist_id_i_arr;
				endif;
				$value->artist = $this->solr->query($artistQuery)->result();
			endif;
			/*Get Album Info*/
			if(!empty($value->album_id_i_arr)):
				$albumQuery = '';
				if(is_array($value->album_id_i_arr)):
					foreach($value->album_id_i_arr as $album_id):
						$albumQuery .= 'id:album_'.$album_id.' OR ';
					endforeach;
					$albumQuery = rtrim($artistQuery,' OR ');
				else:
					$albumQuery = 'id:album_'.$value->album_id_i_arr;
				endif;
				$value->album = $this->solr->query($albumQuery)->result();
			endif;
		endforeach;
		
		$this->load->view("video/ajax_search_result", $this->data);
	}
	
	private function video_cat_header()
	{
/*		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";
		
		if ( !$this->data['slide_show'] = $this->memcache->get('dataVdoSlideRandom')):
			$SQL = "SELECT * FROM (SELECT t.video_id,t.is_relate_song,t.is_publish,t.video_publish_date,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext FROM oni_video as t INNER JOIN oni_video_info as j ON (j.video_id=t.video_id) WHERE t.is_publish = '1' AND t.is_show_display_on_home = '1'  AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW()) ORDER BY t.video_id DESC LIMIT 12) as y WHERE y.is_publish = '1' AND (y.video_publish_date IS NULL OR y.video_publish_date < NOW()) ORDER BY RAND()";
			$this->data['slide_show'] = $this->db->query($SQL)->result();
			foreach($this->data['slide_show'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id))
					->result();				
				endif;
			endforeach;
			//Set Memcache
			$this->memcache->set('dataVdoSlideRandom', $this->data['slide_show'], $this->memcache_time);
		endif;

		if ( !$this->data['slide_mv'] = $this->memcache->get('dataVdoSlideMv')):
			$this->data['slide_mv'] = $this->db->select('t.video_id,k.views,k.is_relate_song,j.video_name,SUM(t.video_click_views) as q_views,j.video_thumb_name,j.video_thumb_ext')->
			join('video as k',"k.video_id=t.video_id AND k.video_type_id = 1",'INNER')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('q_views DESC')->
			group_by('t.video_id')->
			get_where('video_click_stat as t',"t.update_date <= NOW() AND t.update_date >= DATE_SUB(NOW(),INTERVAL 1 MONTH) AND k.is_publish = '1' AND (k.video_publish_date IS NULL OR k.video_publish_date < NOW())",10)->
			result();
			foreach($this->data['slide_mv'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id))
					->result();				
				endif;
			endforeach;
			//Set Memcache
			$this->memcache->set('dataVdoSlideMv', $this->data['slide_mv'], $this->memcache_time);			
		endif;

		if ( !$this->data['slide_concert'] = $this->memcache->get('dataVdoSlideConcert')):
			$this->data['slide_concert'] = $this->db->select('t.video_id,k.views,k.is_relate_song,j.video_name,SUM(t.video_click_views) as q_views,j.video_thumb_name,j.video_thumb_ext')->
			join('video as k',"k.video_id=t.video_id AND k.video_type_id = 4",'INNER')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('q_views DESC')->
			group_by('t.video_id')->
			get_where('video_click_stat as t',"t.update_date <= NOW() AND t.update_date >= DATE_SUB(NOW(),INTERVAL 1 MONTH) AND k.is_publish = '1' AND (k.video_publish_date IS NULL OR k.video_publish_date < NOW())",10)->
			result();
			foreach($this->data['slide_concert'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id))
					->result();				
				endif;
			endforeach;
			//Set Memcache
			$this->memcache->set('dataVdoSlideConcert', $this->data['slide_concert'], $this->memcache_time);			
		endif;

		if ( !$this->data['slide_interview'] = $this->memcache->get('dataVdoSlideInterview')):
			$this->data['slide_interview'] = $this->db->select('t.video_id,k.views,k.is_relate_song,j.video_name,SUM(t.video_click_views) as q_views,j.video_thumb_name,j.video_thumb_ext')->
			join('video as k',"k.video_id=t.video_id AND k.video_type_id = 6",'INNER')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('q_views DESC')->
			group_by('t.video_id')->
			get_where('video_click_stat as t',"t.update_date <= NOW() AND t.update_date >= DATE_SUB(NOW(),INTERVAL 1 MONTH) AND k.is_publish = '1' AND (k.video_publish_date IS NULL OR k.video_publish_date < NOW())",10)->
			result();
			foreach($this->data['slide_interview'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id))
					->result();				
				endif;
			endforeach;
			//Set Memcache
			$this->memcache->set('dataVdoSlideInterview', $this->data['slide_interview'], $this->memcache_time);			
		endif;

		if ( !$this->data['slide_behind_scence'] = $this->memcache->get('dataVdoSlideBHS')):
			$this->data['slide_behind_scence'] = $this->db->select('t.video_id,k.views,k.is_relate_song,j.video_name,SUM(t.video_click_views) as q_views,j.video_thumb_name,j.video_thumb_ext')->
			join('video as k',"k.video_id=t.video_id AND k.video_type_id = 3",'INNER')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			order_by('q_views DESC')->
			group_by('t.video_id')->
			get_where('video_click_stat as t',"t.update_date <= NOW() AND t.update_date >= DATE_SUB(NOW(),INTERVAL 1 MONTH) AND k.is_publish = '1' AND (k.video_publish_date IS NULL OR k.video_publish_date < NOW())",10)->
			result();
			foreach($this->data['slide_behind_scence'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id))
					->result();				
				endif;
			endforeach;
			//Set Memcache
			$this->memcache->set('dataVdoSlideBHS', $this->data['slide_behind_scence'], $this->memcache_time);			
		endif;

		if ( !$this->data['slide_you2playtv'] = $this->memcache->get('dataVdoSlideTv')):
			$this->data['slide_you2playtv'] = $this->db->select('t.video_id,k.views,k.is_relate_song,j.video_name,SUM(t.video_click_views) as q_views,j.video_thumb_name,j.video_thumb_ext')->
			join('video as k',"k.video_id=t.video_id",'INNER')->
			join('video_info as j',"j.video_id=t.video_id",'INNER')->
			join('video_type as l',"l.video_type_id=k.video_type_id",'INNER')->
			order_by('q_views DESC')->
			group_by('t.video_id')->
			get_where('video_click_stat as t',"k.is_publish = '1' AND l.is_tv_show = '1' AND t.update_date <= NOW() AND t.update_date >= DATE_SUB(NOW(),INTERVAL 1 MONTH) AND k.is_publish = '1' AND (k.video_publish_date IS NULL OR k.video_publish_date < NOW())",10)->
			result();
			foreach($this->data['slide_you2playtv'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id))
					->result();				
				endif;
			endforeach;
			//Set Memcache
			$this->memcache->set('dataVdoSlideTv', $this->data['slide_you2playtv'], $this->memcache_time);			
		endif;*/
		
		return $this->load->view($this->router->class."/header_cat", $this->data,TRUE);
	}
	
	private function ajax_video_pager()
	{
		echo $this->video_pager();
		exit;
	}
	
	private function video_pager()
	{
		$group = $this->uri->segment(1);
		$list = $this->uri->segment(2);
		$media_type = $this->uri->segment(3);
		if($group!=NULL && array_key_exists($group,$this->data['group'])):
			$orderBy = "t.video_id DESC";
			$searchCondition = "t.is_publish = '1' AND (t.video_publish_date IS NULL OR t.video_publish_date < NOW())";
			if($list!=NULL):
				if(array_key_exists($list,$this->data['filter'])):
					switch($list):
						case 'new_released':
							$orderBy = "t.video_id DESC";
						break;
						case 'recommend':
							$searchCondition .= " AND t.is_show_recommend = '1'";
						break;
						case 'hit':
							$orderBy = "t.views DESC";
						break;
					endswitch;
					if(is_array($this->data['group'][$group])):
						$dataVideoType = $this->db->select('video_type_id')->get_where('video_type',$this->data['group'][$group])->result();
						$video_type_id = array();
						foreach($dataVideoType as $value):
							$video_type_id[] = $value->video_type_id;
						endforeach;
						$searchCondition .= " AND t.video_type_id IN (".implode(',',$video_type_id).")";
					else:
						$searchCondition .= " AND t.video_type_id = ".$this->data['group'][$group]."";
					endif;
				else:
					$is_exist = $this->db->from('video_type')->where(array('video_type_alias' => $list))->count_all_results();
					if(empty($is_exist)) $this->page_missing();
					
					$searchCondition = "t.video_type_id = ".$this->db->get_where('video_type',array('video_type_alias' => $list),1)->row()->video_type_id."";			
				endif;
			else:
				if(is_array($this->data['group'][$group])):
					$dataVideoType = $this->db->select('video_type_id')->get_where('video_type',$this->data['group'][$group])->result();
					$video_type_id = array();
					foreach($dataVideoType as $value):
						$video_type_id[] = $value->video_type_id;
					endforeach;
					$searchCondition .= " AND t.video_type_id IN (".implode(',',$video_type_id).")";
				else:
					$searchCondition .= " AND t.video_type_id = ".$this->data['group'][$group]."";
				endif;			
			endif;
			if($media_type!=NULL && array_key_exists($media_type,$this->data['media_type'])):
				$searchCondition .= " AND t.media_type_id='".$this->data['media_type'][$media_type]."'";
			endif;
			$config['base_url'] = current_url().'?'.query_string(array('p'));
			$config['total_rows'] = $this->db->from('video as t')->where($searchCondition)->count_all_results();
			$config['per_page'] = 36;
			$config['num_links'] = 5;
			$config['first_link'] = '<span class="arrows first ">&laquo;</span>';
		 	$config['next_link'] = '<span class="arrows next ">&gt;</span>';
		 	$config['prev_link'] = '<span class="arrows prev ">&lt;</span>';
			$config['last_link'] = '<span class="arrows last ">&raquo;</span>';
			$config['page_query_string'] = TRUE;
			$config['query_string_segment'] = 'p';
			$this->pagination->initialize($config);
			$this->data['pager'] = $this->pagination->create_links();
				
			$this->data['dataVideo'] = $this->db->select('t.video_id,t.video_type_id,t.is_relate_song,j.video_name,t.views,j.video_thumb_name,j.video_thumb_ext')->
			join('video_info as j',"j.video_id=t.video_id AND t.is_publish='1'",'INNER')->
			order_by($orderBy)->
			get_where('video as t',$searchCondition,$config['per_page'],intval($this->input->get('p')))->
			result();
			foreach($this->data['dataVideo'] as $value):
				if($value->is_relate_song == '1'):
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
					->join('artist_info as j','j.artist_id=k.artist_id','INNER')
					->get_where('video_song as t',array('video_id' => $value->video_id))
					->result();				
				else:
					$value->artist = $this->db->select('j.artist_id,j.artist_name')
					->join('artist_info as j','j.artist_id=t.artist_id','INNER')
					->get_where('video_artist as t',array('video_id' => $value->video_id,'t.is_default' => '1'))
					->result();				
				endif;
			endforeach;			
		else:
			$this->page_missing();
		endif;
		
		return $this->load->view("video/ajax_video",$this->data,true);
	}
	
	public function mv(){$this->category();}
	public function concert(){$this->category();}
	public function interview(){$this->category();}
	public function behind_scence(){$this->category();}
	public function you2playtv(){$this->category();}
	private function category()
	{
		if($this->input->post('ajax')) $this->ajax_video_pager($this->router->method);
		
		$this->data['module'] = $this->module('video');
		
		$this->data['dataVideo'] = $this->video_pager($this->router->method);
		$this->data['cat_header'] = $this->video_cat_header();

		if($this->router->method == "you2playtv"):
			$this->data['dataYou2playTv'] = $this->db->order_by('video_type_name','ASC')->get_where('video_type',array('is_tv_show' => '1'))->result();
			$this->data['dataYou2playTvName'] = "รายการย้อนหลัง";
			if($this->uri->segment(2) != ""):
				$dataVideoType = $this->db->get_where('video_type',array('video_type_alias' => $this->uri->segment(2)))->row();
				if(!empty($dataVideoType)):
					$this->data['dataYou2playTvName'] = $dataVideoType->video_type_name;
				else:
					$this->page_missing();
				endif;
			endif;
		endif;
		
		$this->data['content'] = $this->router->class."/category";
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}
	
	/*Play Video*/
	public function play($id = NULL,$seo_text = NULL)
	{	
		$is_exist = $this->db->from('video')->where(array('video_id' => $id,'is_publish' => '1'))->count_all_results();
		if(empty($is_exist)) $this->page_missing();	
		
		//Rediect if no seo text
		if(empty($seo_text)):
			$dataResult = $this->db->get_where('video_info',array('video_id' => $id),1)->row();
			redirect($video_url.'play/'.intval($id).'-'.seo_url($dataResult->video_name), 'location', 301);
		endif;			
		
		//Load Javascript
		$this->data['js'][] = "jquery.multipleelements.cycle.min.js";
		$this->data['js'][] = "ZeroClipboard/ZeroClipboard.js";
		
		//Load Media Player
		if($this->agent->is_mobile()):
			$this->data['js'][] = "media/player.js";
		else:
			$this->data['js'][] = "media/jwplayerx.js";
		endif;	
				
		//Load Module
		$this->data['module'] = $this->module('video');
		$this->data['module_facebook_plugins_share'] = $this->module_facebook_plugins_share();
		$this->data['module_facebook_plugins_comment'] = $this->module_facebook_plugins_comment();
		
		//Load Ads
/*		$this->data['dataAds'] = array();
		$dataAds = $this->db->select('ads_id,ads_vast,ads_weight,limit_per_day')
		->get_where('ads',"start_date <= '".date('Y-m-d H:i:s')."' AND end_date > '".date('Y-m-d H:i:s')."' AND (ads_impression = 0 OR ads_impression > views) AND is_video = '1'")
		->result();
		foreach($dataAds as $key => $value):
			if(!empty($value->limit_per_day)):
				$impression_today = intval($this->db->select('SUM(views) as Views')
				->get_where('ads_static',"ads_id = ".$value->ads_id." AND event = 'Impression' AND DATE(date) = '".date('Y-m-d')."'")->row()->Views);
				if($impression_today < $value->limit_per_day):
					$this->data['dataAds'][] = $value;
				endif;			
			else:
				$this->data['dataAds'][] = $value;
			endif;
		endforeach;	*/
		
		//Set Solr Seach
		$searchSolrVideo = "";				
				
		$this->data['dataResult'] = $this->db->select('t.video_id,t.video_type_id,t.is_relate_song,t.rate_score,t.views,t.create_date,j.video_name,j.video_thumb_name,j.video_thumb_ext,j.video_file_name,j.video_file_ext,j.video_description,j.video_embed,j.video_tag,k.genre_name')
		->join('video_info as j','j.video_id=t.video_id','INNER')
		->join('genre as k','k.genre_id=t.genre_id','LEFT')
		->get_where('video as t',array('t.video_id' => $id,'is_publish' => '1'),1)
		->row();

		//Check Autoplay && Tranparent to Embed 
		if(!empty($this->data['dataResult']->video_embed)) $this->data['dataResult']->video_embed = preg_replace('/src=\"(.+?)(\?autoplay=1|)\"/','src="$1?autoplay=1&wmode=transparent"',$this->data['dataResult']->video_embed);
		
		$this->data['dataResult']->lyrics = $this->db->select('j.song_lyrics')
		->join('song_info as j','j.song_id=t.song_id','INNER')
		->get_where('video_song as t',array('t.video_id' => $this->data['dataResult']->video_id,'j.song_lyrics !=' => ''))
		->result();
		
		//High Algorythm
		if($this->data['dataResult']->is_relate_song == '1'):
			//Artist
			$dataArtist = $this->db->select('j.artist_id,j.artist_name')
			->join('song_artist as k',"k.song_id=t.song_id AND is_default='1'",'INNER')
			->join('artist_info as j','j.artist_id=k.artist_id','INNER')
			->get_where('video_song as t',array('video_id' => $this->data['dataResult']->video_id))
			->result();	
			$this->data['dataResult']->artist = array();
			foreach($dataArtist as $artist):
				$this->data['dataResult']->artist[] = array(
					'artist_id' => $artist->artist_id,
					'artist_name' => $artist->artist_name
				);
				
				$searchSolrVideo .= 'artist_id_i_arr:'.$artist->artist_id.' OR ';
			endforeach;
			
			//Album
			$dataAlbum = $this->db->select('k.album_id,k.album_name,k.album_file_name,k.album_file_ext')
			->join('song_album as j','j.song_id=t.song_id','INNER')
			->join('album_info as k','k.album_id=j.album_id','INNER')
			->get_where('video_song as t',array('t.video_id' => $this->data['dataResult']->video_id))
			->result();	
			$this->data['dataResult']->album = array();
			foreach($dataAlbum as $album):
				$this->data['dataResult']->album[] = array(
					'album_id' => $album->album_id,
					'album_name' => $album->album_name,
					'album_file_name' => $album->album_file_name,
					'album_file_ext' => $album->album_file_ext
				);
				
				$searchSolrVideo .= 'album_id_i_arr:'.$album->album_id.' OR ';		
			endforeach;
			
			//Chords
			$this->data['dataResult']->chords = $this->db->select('j.song_name,k.chords_id')
			->join('song_info as j','j.song_id=t.song_id','INNER')
			->join('chords as k','k.song_id=t.song_id','LEFT')
			->get_where('video_song  as t',array('t.video_id' => $this->data['dataResult']->video_id),1)
			->row();				
		else:
			//Artist
			$dataArtist = $this->db->select('j.artist_id,j.artist_name')
			->join('artist_info as j','j.artist_id=t.artist_id','INNER')
			->get_where('video_artist as t',array('video_id' => $this->data['dataResult']->video_id,'t.is_default' => '1'))
			->result();	
			$this->data['dataResult']->artist = array();
			foreach($dataArtist as $artist):
				$this->data['dataResult']->artist[] = array(
					'artist_id' => $artist->artist_id,
					'artist_name' => $artist->artist_name
				);
				
				//$searchSolrVideo .= 'artist_id_i_arr:'.$artist->artist_id.' OR ';
			endforeach;	
			
			//Album
			$dataAlbum = $this->db->select('j.album_id,j.album_name,j.album_file_name,j.album_file_ext')
			->join('album_info as j','j.album_id=t.album_id','INNER')
			->get_where('video_album as t',array('t.video_id' => $this->data['dataResult']->video_id))
			->result();	
			$this->data['dataResult']->album = array();
			foreach($dataAlbum as $album):
				$this->data['dataResult']->album[] = array(
					'album_id' => $album->album_id,
					'album_name' => $album->album_name,
					'album_file_name' => $album->album_file_name,
					'album_file_ext' => $album->album_file_ext
				);
				
				//$searchSolrVideo .= 'album_id_i_arr:'.$album->album_id.' OR ';		
			endforeach;
			$tag_arr = explode(',',$this->data['dataResult']->video_tag);
			if(!empty($tag_arr[0])) foreach($tag_arr as $value):
				if(!preg_match('/:|\(|\)/',trim($value))): //Disallow collon
					$searchSolrVideo .= 'video_tag_s:'.$value.' OR ';
				endif;	
			endforeach;						
		endif;		
		
		if(!empty($searchSolrVideo)):
			$searchSolrVideo = 'id:video_* AND last_modified:[* TO '.date("Y-m-d\TH:i:s\Z",time()).'] AND ('.rtrim($searchSolrVideo,' OR ').')';	
		else:
			$searchSolrVideo = 'id:video_* AND last_modified:[* TO '.date("Y-m-d\TH:i:s\Z",time()).']';
		endif;

		//Total Video Click Rate
		$this->data['dataResult']->video_rate_all = @$this->db->from('video_rating')
		->where(array('video_id' => $id))
		->count_all_results();
		
		//Load Library
		$this->load->library('solr');
		if($this->data['dataResult']->is_relate_song == '1'):
			$this->data['dataVideoRelate'] = $this->solr->query($searchSolrVideo)->limit(9)->order_by(array('last_modified' => 'DESC'))->result();
		else:
			$this->data['dataVideoRelate'] = $this->solr->query($searchSolrVideo)->limit(9)->result();
		endif;
		
		if(count($this->data['dataVideoRelate']) == 0):
			$searchSolrVideo = 'id:video_*';
			$this->data['dataVideoRelate'] = $this->solr->query($searchSolrVideo)->limit(9)->order_by(array('last_modified' => 'DESC'))->result();
		endif;
		foreach($this->data['dataVideoRelate'] as $value):
			/*Get Artist Info*/
			if(!empty($value->artist_id_i_arr)):
				$artistQuery = '';
				if(is_array($value->artist_id_i_arr)):
					foreach($value->artist_id_i_arr as $artist_id):
						$artistQuery .= 'id:artist_'.$artist_id.' OR ';
					endforeach;
					$artistQuery = rtrim($artistQuery,' OR ');
				else:
					$artistQuery = 'id:artist_'.$value->artist_id_i_arr;
				endif;
				$value->artist = $this->solr->query($artistQuery)->result();
			endif;			
		endforeach;

		//Set SEO Tag
		$this->data['seo_title'] = $this->data['dataResult']->video_name.(!empty($this->data['dataResult']->artist) ? ' - '.$this->data['dataResult']->artist{0}['artist_name']:'').' | วีดีโอ คลิปวีดีโอ MV  You2Play.com';
		$this->data['seo_description'] = ''.$this->data['dataResult']->video_name.(!empty($this->data['dataResult']->artist) ? ' วง '.$this->data['dataResult']->artist{0}['artist_name']:'').' '.$this->data['seo_description'];
		$this->data['seo_keyword'] = ($this->data['dataResult']->video_tag ? rtrim($this->data['dataResult']->video_tag,','):'').', วิดีโอ, คลิปวีดีโอ, MV';	
		$this->data['fb_title'] = $this->data['seo_title'];
		if(!empty($this->data['dataResult']->video_thumb_name)) $this->data['fb_image'] = image_asset_url('../uploads/video/images/120x120/'.$this->data['dataResult']->video_thumb_name.'.'.$this->data['dataResult']->video_thumb_ext);
		if(!empty($this->data['dataResult']->video_embed)):
			if(preg_match('/\/embed\/(.+?)\"/',$this->data['dataResult']->video_embed,$matches)):
				$this->data['fb_media'] = 'http://www.youtube.com/v/'.$matches[1].'?version=3&amp;autohide=1';
			endif;
		else:
			$image_file = "";
			if(!empty($this->data['dataResult']->video_thumb_name)) $image_file = image_asset_url('../uploads/video/images/original/'.$this->data['dataResult']->video_thumb_name.'.'.$this->data['dataResult']->video_thumb_ext);		
			$media_file = other_asset_url('uploads/video/files/'.date('Y/m/d',strtotime($this->data['dataResult']->create_date)).'/'.$this->data['dataResult']->video_file_name.'.'.$this->data['dataResult']->video_file_ext);
			$this->data['fb_media'] = 'http://static.you2play.com/2013/assets/js/media/player.swf?file='.$media_file.'&image='.$image_file.'&autostart=true';
		endif;
		$this->data['fb_type'] = 'video';
		
		//GA Events
		$this->data['ga_event'] = 'CI-'.$id.'-'.seo_url($this->data['dataResult']->video_name);	
		
		//Hawkeye Category
		if(isset($this->data['category_hawkeye'][$this->data['dataResult']->video_type_id])):
			$this->data['hawkeye_category_name'] = $this->data['category_hawkeye'][$this->data['dataResult']->video_type_id];
		endif;	
			
		$this->data['content'] = $this->router->class."/".$this->router->method;
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);
	}

	/*Ajax View Video*/
	public function ajax_view_video()
	{
		$this->video_model->ajax_view_video();
	}
	
	/*Ajax Vote Video*/
	public function ajax_vote_video()
	{
		$this->video_model->ajax_vote_video();
	}
	
	/*Ajax Copy Stat*/
	public function ajax_copy_stat()
	{
		$this->video_model->ajax_copy_stat();
	}	
}


/* End of file video.php */
/* Location: ./application/controllers/video.php */